package fun.joney.controller;

import fun.joney.Main;
import fun.joney.listener.MyTaskListener;
import fun.joney.task.MyTask;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author wangxianchen
 * @create 2018-06-07
 * @desc
 */
public class Second extends VBox implements Initializable {

    private Main application;

    public void setApp(Main application){
        this.application = application;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("initialize");
    }

    @FXML
    public void quit(ActionEvent event){
        application.gotoFirst();
    }

    @FXML
    public void openMask(ActionEvent event){
        MyTask task = new MyTask();
        task.valueProperty().addListener(new MyTaskListener());
        application.showMask(task);
    }

    @FXML
    public void clickBox(MouseEvent event){
        Scene node = application.getStage().getScene();
        System.out.println(2323424);
    }
    @FXML
    public void about(ActionEvent event){
        String fxml = "/about.fxml";
        FXMLLoader loader = new FXMLLoader();
        InputStream in = this.getClass().getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(this.getClass().getResource(fxml));
        VBox page = null;
        try {
            page = (VBox) loader.load(in);
            in.close();
        }catch (Exception e){
            e.printStackTrace();
        }

        Popup pp = new Popup();
        pp.setAutoHide(true);
        pp.getContent().add(page);
        Scene scene = application.getStage().getScene();
        //System.out.println(scene.getWindow().getX()+"--"+scene.getWindow().getY());
        //System.out.println(scene.getX()+"--"+scene.getY());
        //System.out.println(this.localToScene(0, 0).getX()+"--"+this.localToScene(0, 0).getY());
        //System.out.println(scene.getWindow().getWidth());
        double screenX = scene.getWindow().getX() + scene.getX() + this.localToScene(0, 0).getX() + scene.getWindow().getWidth()/2 - page.getPrefWidth()/2;
        double screenY = scene.getWindow().getY() + scene.getY() + this.localToScene(0, 0).getY() + scene.getWindow().getHeight()/2 - page.getPrefHeight()/2 - 10;
/*        page.getChildren().forEach(obj -> {
              if(obj instanceof Label){
                  Label label = (Label) obj;
                  label.setText("screenX:"+screenX+" screenY:"+screenY);
              }
            }
        );*/
        pp.setX(screenX);
        pp.setY(screenY);
        pp.show(scene.getWindow());
    }
}
 /*       Platform.runLater(new Runnable() {
            @Override
            public void run() {
//更新JavaFX的主线程的代码放在此处

        });
*/